package br;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class trasa2
 */
@WebServlet("/trasa2")
public class trasa2 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public trasa2() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 response.setCharacterEncoding("UTF-8");
		 response.setContentType("text/html;charset=UTF-8"); 
		HttpSession sesja = request.getSession(true); 
			PrintWriter out = response.getWriter();
			System.out.println(sesja.getAttribute("log")+" "+sesja.getAttribute("nick"));
			if(sesja.getAttribute("log").equals("admin")){ 
			 String nick = request.getParameter("idkuriera");
			 System.out.println(nick);
			 String odp="";
			 String tekst = new String(request.getParameter("tekst").getBytes("ISO8859_1"), "UTF8");
			 System.out.println(tekst);
			 String adresy[] = tekst.split("\\|");
			 Calendar now = Calendar.getInstance();
			 now.set(2011, 12, 22, 8, 0, 0);
			 for(int i=0;i<adresy.length;i++){
				 String dobazy[]=adresy[i].split(";");
				 String adres=dobazy[1];
				 int time = Integer.parseInt(dobazy[2]);
				 int idzam=0;
				 if(i>0){
					 try {
						  Connection con =mb.commons.DbConnection.getConnection();
						  Statement stmt = con.createStatement();
						  ResultSet rs = stmt.executeQuery("SELECT zamowienia.id_zamowienia FROM zamowienia,kurierzy,paczki WHERE paczki.id_paczki=zamowienia.id_paczki AND kurierzy.id_kuriera=zamowienia.id_kuriera AND kurierzy.id_kuriera="+nick+" AND paczki.adres2 LIKE '%"+adres.split(",")[0]+"%'  AND zamowienia.status='dowoz do klienta'");						  
						  while(rs.next()){
							idzam = rs.getInt("id_zamowienia");
							Calendar cal = Calendar.getInstance();
							SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
							Connection con2 =mb.commons.DbConnection.getConnection();
							  Statement stmt2 = con2.createStatement();
							   stmt2.executeUpdate("INSERT INTO zamowienia_czas VALUES (NULL,"+idzam+",'"+now.get(Calendar.HOUR_OF_DAY)
				                      + ":"
				                      + now.get(Calendar.MINUTE)
				                      + ":"
				                      + now.get(Calendar.SECOND)+"','"+sdf.format(cal.getTime())+"')");
						  }
						  
					 }catch(Exception e){
						 e.printStackTrace();
					 }
				 System.out.println(adres+" "+ now.get(Calendar.HOUR_OF_DAY)
	                      + ":"
	                      + now.get(Calendar.MINUTE)
	                      + ":"
	                      + now.get(Calendar.SECOND));
				 }
				 now.add(Calendar.SECOND, time);
			 }
				
		}
		}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
